package com.xiedafeng.www.view;

import com.xiedafeng.www.controller.CircleController;
import com.xiedafeng.www.po.Circle;
import com.xiedafeng.www.po.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.List;

public class CircleView {
    CircleController circleController=new CircleController();
    Stage stage=new Stage();
    public void showCircle(User user,List<Circle> circleList) {
        Button createCircle=new Button("创建瓜圈");
        GridPane gridPane=new GridPane();
        gridPane.add(createCircle,0,0);
        for (int i = 1; i <= circleList.size(); i++) {
            Circle circle=circleList.get(i-1);
            Text tip1=new Text("圈名");
            gridPane.add(tip1,0,i*3-2);
            Text circleName=new Text();
            circleName.setText(circle.getCircleName());
            gridPane.add(circleName,1,i*3-2);
            Text tip2=new Text("时间");
            gridPane.add(tip2,0,i*3-1);
            Text createtime=new Text();
            createtime.setText(circle.getCircleCreatetime());
            gridPane.add(createtime,1,i*3-1);
            Button enter=new Button("进入瓜圈");
            gridPane.add(enter,0,i*3);
            Button delete=new Button("删除瓜圈");
            gridPane.add(delete,1,i*3);
            enter.setOnMouseClicked(event -> {
                try {
                    enter(user,circle);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            delete.setOnMouseClicked(event -> {
                try {
                    delete(user,circle);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        createCircle.setOnMouseClicked(event -> {
            try {
                checkRole(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        gridPane.setAlignment(Pos.CENTER);
        Scene scene=new Scene(gridPane);
        stage.setScene(scene);
        stage.showAndWait();
    }
    private void enter(User user,Circle circle) throws Exception {
        circleController.showCircleEventView(user,circle);
    }
    private void checkRole(User user) throws Exception {
        circleController.checkRole(user);
    }
    private void delete(User user,Circle circle) throws Exception {
        circleController.deleteCircle(user,circle);
    }
}
